package com.springboot.framework.mvc.mapper;

import com.springboot.framework.mvc.mapper.driver.BaseMapperDriver;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 基础baseMapper,提取mapper公用的方法
 * 参数为实体类或者map的不能用  @Param 注解
 * @param <T>
 */
public interface BaseMapper<T> {

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Lang(BaseMapperDriver.class)
    @Select("SELECT ${columns} FROM ${table} where ${id} = #{id}")
    T getById(@Param("id") int id);

    /**
     * 条件查询,参数为map,
     * 注意: 不能使用 @Param 注解,key的值必须为实体类的字段名
     * @return
     */
    @Lang(BaseMapperDriver.class)
    @Select("SELECT ${columns} FROM ${table} where 1=1 ${conditions}")
    List<T> getByCondition(Map paramsMap);

    /**
     * 优先使用此方法做查询
     * 条件查询,参数为实体类,不能使用 @Param 注解
     * @return
     */
    @Lang(BaseMapperDriver.class)
    @Select("SELECT * FROM ${table} where 1=1 ${conditions} ")
    List<T> getByCondition(T model);

    /**
     * 插入
     * @param model
     * @return
     */
    @Lang(BaseMapperDriver.class)
    @Insert("INSERT INTO ${table} ${keys} values ${values}")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    Long insert(T model);

    /**
     * 根据id 修改
     * @param model
     * @return
     */
    @Lang(BaseMapperDriver.class)
    @Update("UPDATE ${table} ${sets} WHERE ${id}=#{id}")
    Long updateById(T model);

    /**
     * 删除
     * @param id
     * @return
     */
    @Lang(BaseMapperDriver.class)
    @Delete("DELETE FROM ${table} WHERE ${id}=#{id}")
    Long deleteById(@Param("id") int id);

    /**
     * 判断是否存在
     * @param id
     * @return
     */
    @Lang(BaseMapperDriver.class)
    @Select("SELECT COUNT(1) FROM ${table} WHERE ${id}=#{id}")
    Boolean existById(@Param("id") int id);
}